﻿using Caliburn.Micro;
using 行情显示.数据模板.Models;

namespace 行情显示.数据中心.ViewModels
{
    public class MainWindowViewModel : Screen
    {
        #region Property Source
        /// <summary>
        /// Source
        /// </summary>
        public BindableCollection<行情> Source
        {
            get => this._source;
            set => this.Set(ref this._source, value);
        }

        private BindableCollection<行情> _source = new BindableCollection<行情>();
        #endregion

        #region ctor
        public MainWindowViewModel()
        {
        }
        #endregion

        #region Key Method
        public void EnterPressed()
        { }

        public void CtrlEnterPressed()
        { }
        #endregion

        System.Random rd = new System.Random();
        bool isStart = false;

        #region Method Add
        private long tem;
        public bool CanAdd(string num) => !string.IsNullOrEmpty(num) && long.TryParse(num, out tem);
        /// <summary>
        /// Add
        /// </summary>
        async public void Add(string num)
        {
            for (int i = 0; i < long.Parse(num); i++)
            {
                await System.Threading.Tasks.Task.Delay(1);
                this.Source.Add(new 行情
                {
                    Title = $"股票 {rd.Next()}",
                });
            }
        }
        #endregion

        #region Method Random
        /// <summary>
        /// Random
        /// </summary>
        async public void Random()
        {
            if (isStart) return;
            isStart = true;
            while (isStart)
            {
                RandomSource();
                await System.Threading.Tasks.Task.Delay(100);
            }
        }
        #endregion

        #region Method EndRandom
        /// <summary>
        /// EndRandom
        /// </summary>
        public void EndRandom()
        {
            isStart = false;
        }
        #endregion

        #region Method 是否全选
        /// <summary>
        /// 是否全选
        /// </summary>
        public void 是否全选(bool isck)
        {
            if (this.Source == null) return;
            foreach (var item in this.Source)
            {
                item.IsChecked = isck;
            }
        }
        #endregion

        private void RandomSource()
        {
            foreach (var item in this.Source)
            {
                item.Price = rd.Next();
                item.PrePrice = rd.Next();
                item.Stock = rd.Next();
                item.PreStock = rd.Next();
            }
        }
    }
}
